import GamePanel from "../../hall/gamePanel";

const { ccclass, property } = cc._decorator;

@ccclass
export default class game1000 extends cc.Component {
    @property(cc.Label)
    score_label: cc.Label = null
    @property(cc.Label)
    max_label: cc.Label = null
    @property(cc.Node)
    player: cc.Node = null;
    @property(cc.Node)
    content: cc.Node = null;
    @property(cc.Node)
    modes: cc.Node = null;

    clickstate: boolean = true;
    anglestate: boolean = true;     //角色转动状态
    anglefx: number = -1;           //角色转动方向
    usecount: number = 0;//无效次数
    protected onLoad(): void {
        cc.director.getCollisionManager().enabled = true;
        cc.director.getCollisionManager().enabledDebugDraw = false;
        this.initData()
    }
    /**初始化数据 */
    initData() {
        this.max_label.string = '0'
        this.score_label.string = '0'
        this.node.on('touchend', () => {
            if (!this.clickstate) { return; }
            this.down();
        })
        this.rndfish();
    }

    protected update(dt: number): void {
        if (!this.anglestate) { return; }
        let v = dt * 50
        if (this.anglefx == -1) {
            this.player.angle -= v;
        }
        if (this.anglefx == 1) {
            this.player.angle += v;
        }
        if (this.player.angle > 40) { this.anglefx = -1 }
        if (this.player.angle < -130) { this.anglefx = 1 }
    }

    //随机鱼
    rndfish() {
        this.schedule(() => {
            if (this.content.childrenCount > 10) { return; }
            let index = Math.floor(Math.random() * this.modes.childrenCount)
            let mode = this.modes.children[index]
            let Tnode = cc.instantiate(mode)
            Tnode.parent = this.content;
            Tnode.getComponent('game100fish').initMgr();
        }, 1)
    }
    //下网
    down() {
        // console.log('下网')
        this.player.getChildByName('wang').group = 'myplayer'
        this.clickstate = false;
        this.anglestate = false;
        cc.tween(this.player)
            .to(0.5, { height: 1500 })
            .call(() => {
                this.player.getChildByName('wang').group = 'default'
            })
            .to(1.5, { height: 30 })
            .call(() => { this.anglestate = true; this.clickstate = true; })
            .start();
    }
    //收网
    jump(callback?) {
        this.player.getChildByName('wang').group = 'default'
        this.player.stopAllActions();
        cc.tween(this.player)
            .to(0.7, { height: 30 })
            .call(() => {
                this.anglestate = true; this.clickstate = true;
                if (callback) { callback() }
                if (this.usecount >= 3) {
                    this.checkGameOver()
                }
            })
            .start();
    }

    isGameOver: boolean = false
    checkGameOver() {
        if (this.isGameOver) {
            return
        }
        this.isGameOver = true
        this.schedule(() => {
            GamePanel.instance.showGameStatis()
        }, 1, 0)
    }
}
